const { ref } = Vue
import { useFetch } from '../../../Scripts/hooks/useFetch.js'
const { ElMessage } = ElementPlus

export function useDocumentView() {
  let treeData = ref([]);//文档查看Tree数据
  let documentList = ref([]);//文档查看文件清单
  let documentViewDialogVisible = ref(false);

  // 获取工艺信息
  const getDocumentViewTreeData = async configObj => {
    const { currentSelectContainer, baseAPI } = configObj;
    if (!currentSelectContainer.productid) {
      return ElMessage({ type: 'error', message: `请选择批次`, grouping: true });
    }
    documentViewDialogVisible.value = true;
    documentList.value = [];// 清空文档清单
    const { productid, workflowid } = currentSelectContainer;
    const { data } = await useFetch({
      url: `${baseAPI}?actions=getDocumentViewTreeData`,
      data: { type: 'getDocumentViewTreeData', productid, workflowid }
    });
    treeData.value = data.value;
  };

  // 获取文档list
  const getDocumentList = async configObj => {
    const { data } = await useFetch({
      url: `${configObj.url}?actions=${configObj.method}`,
      data: {
        type: `${configObj.method}`,
        id: configObj.id,
        nodeType: configObj.nodeType
      }
    });
    if (data.value.length === 0) {
      documentList.value = [];
      return ElMessage({ type: 'error', message: `未查询到文档信息`, grouping: true })
    }
    documentList.value = data.value;
  }

  // 文档下载
  const downloadFile = async configObj => {
    const { data } = await useFetch({
      url: `${configObj.url}?actions=${configObj.method}`,
      data: {
        type: `${configObj.method}`,
        filepath: configObj.filepath
      }
    });
    window.open(data.value);
  }

  return { treeData, documentList, documentViewDialogVisible, getDocumentViewTreeData, getDocumentList, downloadFile };
}